package com.example.mobile1920605116cxb0603homework;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private SurfaceView svMainVideo;
    private Button btnMainPlay;
    SurfaceHolder holder;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        holder=svMainVideo.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.video);
                mediaPlayer.setDisplay(holder);
                mediaPlayer.start();
                btnMainPlay.setText("暂停");
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }
        });
        btnMainPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                    btnMainPlay.setText("播放");
                }else{
                    mediaPlayer.start();
                    btnMainPlay.setText("暂停");
                }
            }
        });
    }

    private void initView() {
        svMainVideo = (SurfaceView) findViewById(R.id.svMainVideo);
        btnMainPlay = (Button) findViewById(R.id.btnMainPlay);
    }
}
